# Maintainer: Ronald van Haren <ronald.archlinux.org>
# Maintainer: Gaetan Bisson <bisson@archlinux.org>
# Contributor: Jaroslav Lichtblau <dragonlord@aur.archlinux.org>
# Contributor: Anton Leontiev <bunder@t-25.ru>

pkgname=graphicsmagick
pkgver=1.3.36
pkgrel=3
pkgdesc='Image processing system'
url='http://www.graphicsmagick.org/'
arch=('x86_64')
license=('MIT')
optdepends=('jasper: jp2 module'
            'libwmf: wmf module'
            'libxml2: msl, svg, url modules'
            'ghostscript: pdf, ps modules')
makedepends=('perl' 'jasper' 'libwmf' 'libxml2')
depends=('freetype2' 'libxext' 'libsm' 'lcms2' 'libltdl' 'bzip2' 'xz'
         'libpng' 'libtiff' 'libwebp')
source=("https://downloads.sourceforge.net/project/${pkgname}/${pkgname}/${pkgver}/GraphicsMagick-${pkgver}.tar.xz")
sha256sums=('5d5b3fde759cdfc307aaf21df9ebd8c752e3f088bb051dd5df8aac7ba7338f46')

options=('!emptydirs' 'libtool')

prepare() {
	cd "${srcdir}/GraphicsMagick-${pkgver}"
	sed -e "s:freetype_config='':freetype_config='/usr/bin/pkg-config freetype2':g" -i configure
}

build() {
	cd "${srcdir}/GraphicsMagick-${pkgver}"
	./configure \
		--prefix=/usr \
		--enable-shared \
		--with-modules \
		--with-perl \
		--with-quantum-depth=16 \
		--with-threads
	make
}

package() {
	cd "${srcdir}/GraphicsMagick-${pkgver}"

	make DESTDIR="${pkgdir}" install

	# Install MIT license
	install -Dm644 "Copyright.txt" "${pkgdir}/usr/share/licenses/${pkgname}/Copyright.txt"

	# Install perl bindings
	# The patching was introduced in order to build perl module without installing package itself and
	# not to introduce unnecessary path into LD_RUN_PATH
	cd PerlMagick
	sed -i -e "s:'LDDLFLAGS'  => \"\(.*\)\":'LDDLFLAGS'  => \"-L${pkgdir}/usr/lib \1\":" Makefile.PL
	perl Makefile.PL INSTALLDIRS=vendor PREFIX=/usr DESTDIR="${pkgdir}"
	sed -i -e "s/LDLOADLIBS =/LDLOADLIBS = -lGraphicsMagick/" Makefile
	make
	make install

	# Remove perllocal.pod and .packlist
	rm -r "${pkgdir}"/usr/lib/perl5/*/core_perl
	rm "${pkgdir}"/usr/lib/perl5/*/vendor_perl/auto/Graphics/Magick/.packlist
}
